import http from '@utils/request'
interface Irole {
  current: number
  size: number
  roleName?: string
  rolePerm?: string
  enabled?: string
}

export interface Role {
  id: string
  roleName: string
  rolePerm: string
  unitId: string
  dataPrivileges: number
  enabled: number
  createBy: string | null
  createTime: number | null
  updateBy: string | null
  updateTime: number | null
  descript: string | null
}

export interface IroleApiResponse {
  code: string
  msg: string
  data: {
    records: Role[]
    total: number
    size: number
    current: number
    orders: any[]
    optimizeCountSql: boolean
    searchCount: boolean
    countId: string | null
    maxLimit: number | null
    pages: number
  }
}
// 菜单权限数据
export interface IRoleMenuItem {
  id: string
  name: string
  parentId: string
  sort: number
  path: string
  query: any
  component: string
  cache: number
  type: number
  visible: number
  redirect: string
  enabled: number
  perms: string
  icon: string
  remark: string | null
  createBy: string | null
  createTime: string | null
  updateBy: string | null
  updateTime: string | null
  children?: IRoleMenuItem[]
}
export interface IRoleMenu {
  code: string
  msg: string
  data: IRoleMenuItem[]
}

// 添加角色数据
export interface IRoleAdd {
  id: string
  roleName: string
  rolePerm: string
  enabled: string
  descript: string
  permissionIds: string[]
}
//添加角色返回的参数
interface IRoleAddData {
  code: number
  msg: string
  data: null
}

// 删除类型
interface IroleDel {
  code: string
  msg: string
  data: null
}

// 角色编辑侧类型
export interface Roles {
  id: string // 角色 ID
  roleName: string // 角色名称
  rolePerm: string // 角色权限字符
  unitId: string // 机构 ID
  dataPrivileges: number // 数据权限
  enabled: string // 是否启用（0：禁用；1：启用）
  createBy: null | string
  createTime: number
  updateBy: null | string
  updateTime: null | number
  descript: null | string // 描述
}

// 定义响应数据的接口
export interface ResponseData {
  role: Role
  permissions: string[]
}

// 定义完整响应的接口
export interface ApiResponse {
  code: string
  msg: string
  data: ResponseData
}

// 获取角色列表
export const rolePage = async (params: Irole): Promise<IroleApiResponse> => {
  return await http.get<IroleApiResponse>('/system/role/page', { params })
}

// //获取菜单树
export const menuTree = async (): Promise<IRoleMenu> => {
  return await http.get<IRoleMenu>('/system/menu/tree')
}

// 添加校色列表
export const roleAdd = async (data: IRoleAdd): Promise<IRoleAddData> => {
  return await http.post<IRoleMenu>('/system/role/add', data)
}
// 删除数据
export const roleDelete = (data: string): Promise<IroleDel> => {
  return http.get<IroleDel>(`/system/role/delete/${data}`)
}

// 角色编辑
export const roleget = async (id: string): Promise<ApiResponse> => {
  return await http.get<ApiResponse>(`/system/role/get/${id}`)
}

//修改角色
export const updateRole = async (data: IRoleAdd): Promise<IroleDel> => {
  return await http.post<IRoleMenu>('/system/role/update', data)
}
